package com.jintoufs.web.action.quartz;

import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.sys.Dictionary;
import com.jintoufs.domain.user.User;
import com.jintoufs.service.baseinfo.TickerService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.service.user.UserService;
import com.jintoufs.util.MailSendUtils;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.web.action.support.SupportAction;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoemo on 2017/11/23.
 *
 * @author xiaoemo
 */
public class ProfitQuartzAction extends SupportAction {
    @Resource
    private TickerService tickerService;
    @Resource
    private UserService userService;
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private MailSendUtils mailSendUtils;

    public void lastProfit() {
        List<Dictionary> list = dictionaryService.getDicByKeyNo("PROFIT_RATE");
        double profitRate = 0.03;
        if (list != null && list.size() > 0) {
            profitRate = Double.parseDouble(list.get(0).getKeyValue());
        }
        List<Dictionary> list2 = dictionaryService.getDicByKeyNo("trade_volume");
        double tradeVolume = 100000;
        if (list2 != null && list2.size() > 0) {
            tradeVolume = Double.parseDouble(list2.get(0).getKeyValue());
        }

        Map<String, Object> siteMap = new HashMap<>();
        siteMap.put("status", 1);
        //查询钱10分钟内的数据
        String min = DateUtils.getDateBeforeMin(Constants.TICKER_BEFORE_MIN);
        Map siteAmap = new HashMap();
        siteAmap.put("startTime", min);
        siteAmap.put("withdrawStatus", Constants.SITE_COIN_STATUS_ENABLE);
        List<Map<String, Object>> listA = tickerService.getLastestPrice(siteAmap);
        Map siteBmap = new HashMap();
        siteBmap.put("startTime", min);
        siteBmap.put("rechargeStatus", Constants.SITE_COIN_STATUS_ENABLE);
        List<Map<String, Object>> listB = tickerService.getLastestPrice(siteBmap);
        User user = userService.queryByPrimaryKey(1);
        List<Map> result=new ArrayList<Map>();
        for (Map<String, Object> map : listA) {
            for (Map<String, Object> map2 : listB) {
                long id = Long.parseLong(map.get("id").toString());
                long id2 = Long.parseLong(map2.get("id").toString());
                String siteName = map.get("site_name").toString();
                String siteName2 = map2.get("site_name").toString();
                if (id == id2 || siteName.equals(siteName2)) {
                    continue;
                }
                double last = Double.parseDouble(map.get("sell").toString());
                String coinName = map.get("coin_rename").toString();
                String uniteCode = map.get("unit_code").toString();
                String sell_trade_fee1 = map.get("sell_trade_fee").toString();
                String buy_trade_fee1 = map.get("buy_trade_fee").toString();
//                String miner_fee1 = map.get("miner_fee").toString();
//                String take_out_fee1 = map.get("take_out_fee").toString();
                String date = map.get("date").toString();
                double vol1 = Double.parseDouble(map.get("vol").toString());
                double sell1 = Double.parseDouble(map.get("sell").toString());
                double buy1 = Double.parseDouble(map.get("buy").toString());
                String recharge_status1 = String.valueOf(map.get("recharge_status").toString());
                String withdraw_status1 = String.valueOf(map.get("withdraw_status").toString());


                double last2 = Double.parseDouble(map2.get("buy").toString());
                String coinName2 = map2.get("coin_rename").toString();
                String uniteCode2 = map2.get("unit_code").toString();
                String sell_trade_fee2 = map2.get("sell_trade_fee").toString();
                String buy_trade_fee2 = map2.get("buy_trade_fee").toString();
//                String miner_fee2 = map2.get("miner_fee").toString();
//                String take_out_fee2 = map2.get("take_out_fee").toString();
                String date2 = map2.get("date").toString();
                double vol2 = Double.parseDouble(map2.get("vol").toString());
                double sell2 = Double.parseDouble(map2.get("sell").toString());
                double buy2 = Double.parseDouble(map2.get("buy").toString());
                String recharge_status2 = String.valueOf(map2.get("recharge_status").toString());
                String withdraw_status2 = String.valueOf(map2.get("withdraw_status").toString());
                //属于同一个币种，则进行比较
                // 转换为CNY后计算
                double price1 = 0, price2 = 0;
                if (coinName.equalsIgnoreCase(coinName2)) {
                    price2 = getPrice(uniteCode2, listB);
                    price1 = getPrice(uniteCode, listA);
                    Map<String, Object> entity = new HashMap();
                    entity.put("siteName1", siteName);
                    entity.put("coinName1", coinName);
                    entity.put("unitCode1", uniteCode);
                    entity.put("sell_trade_fee1", sell_trade_fee1);
                    entity.put("buy_trade_fee1", buy_trade_fee1);
//                    entity.put("take_out_fee1", take_out_fee1);
//                    entity.put("miner_fee1", miner_fee1);
                    entity.put("date1", DateUtils.getDateStringFromString(date, DateUtils.HHMM));
                    entity.put("last1", last);
                    entity.put("vol1", vol1);
                    entity.put("cnyPrice1", last * price1);
                    entity.put("siteName2", siteName2);
                    entity.put("coinName2", coinName2);
                    entity.put("unitCode2", uniteCode2);
                    entity.put("sell_trade_fee2", sell_trade_fee2);
                    entity.put("buy_trade_fee2", buy_trade_fee2);
                    entity.put("date2", DateUtils.getDateStringFromString(date2, DateUtils.HHMM));
                    entity.put("last2", last2);
                    entity.put("vol2", vol2);
                    entity.put("cnyPrice2", last2 * price2);
//                    entity.put("take_out_fee2", take_out_fee2);
//                    entity.put("miner_fee2", miner_fee2);
                    double profit = (last2 * price2 - last * price1) / (last * price1);
                    double tradeVol1 = vol1 * price1;
                    double tradeVol2 = vol2 * price2;
                    entity.put("profit", profit * 100);
                    double pure= (buy2 * price2 - sell1 * price1) / (sell1*price1)-Double.parseDouble(buy_trade_fee1)-Double.parseDouble(sell_trade_fee2) ;
                    entity.put("pure_profit",pure);
                    if (profit >= profitRate&&tradeVol1>tradeVolume&&tradeVol2>tradeVolume&&withdraw_status1.equals(Constants.SITE_STATUS_1)&&recharge_status2.equals(Constants.SITE_STATUS_1)) {// &&vol1*price1>100000&&vol2*price2>100000
                        Map profitMap = new HashMap();
                        profitMap.put("siteAName", siteName);
                        profitMap.put("siteBName", siteName2);
                        profitMap.put("coinName", coinName);
                        profitMap.put("profit", profit*100);
                        profitMap.put("vol1", tradeVol1);
                        profitMap.put("vol2", tradeVol2);
                        profitMap.put("pureProfit", pure*100);
                        result.add(profitMap);
                    }
                }
            }
        }
        Map resultMap= new HashMap();
        resultMap.put("result",result);
        if(result.isEmpty()||result.size()==0)return;
        mailSendUtils.sendMail("有利润了，搬砖啊", resultMap, "jasonimg@gmail.com", "jiaojie.ftl");
        mailSendUtils.sendMail("有利润了，搬砖啊", resultMap, "82517876@qq.com", "jiaojie.ftl");
        mailSendUtils.sendMail("有利润了，搬砖啊", resultMap, "66846355@qq.com", "jiaojie.ftl");
        mailSendUtils.sendMail("有利润了，搬砖啊", resultMap, "yanghao_2011@yeah.net", "jiaojie.ftl");

    }
}
